import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-12-30
 * Time: 22:24
 */
public class Demo {
//    private int a;
//    private String b;
//
//    @Override
//    public boolean equals(Object o) {
//        if (this == o) return true;
//        if (o == null || getClass() != o.getClass()) return false;
//        Demo demo = (Demo) o;
//        return a == demo.a && Objects.equals(b, demo.b);
//    }
//
//    @Override
//    public int hashCode() {
//        return Objects.hash(a, b);
//    }

    public static void main(String[] args) {
        solve("(2+3)*5");
    }
    public static int solve (String s) {
        // write code here
        s = s.trim();
        Deque<Integer> stack = new ArrayDeque<>();
        int number = 0;
        char sign = '+';
        char[] charArray = s.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            char c = charArray[i];
            if (c == ' ') {
                continue;
            }
            if (Character.isDigit(c)) {
                number = number * 10 + c - '0';
            }
            if (c == '(') {
                int j = i + 1;
                int counterPartition = 1;
                while (counterPartition > 0) {
                    if (charArray[j] == '(') {
                        counterPartition++;
                    }
                    if (charArray[j] == ')') {
                        counterPartition--;
                    }
                    j++;
                }
                number = solve(s.substring(i + 1, j - 1));
                i = j - 1;
            }
            if (!Character.isDigit(c) || i == charArray.length - 1) {
                if (sign == '+') {
                    stack.push(number);
                } else if (sign == '-') {
                    stack.push(-1 * number);
                } else if (sign == '*') {
                    stack.push(stack.pop() * number);
                } else if (sign == '/') {
                    stack.push(stack.pop() / number);
                }
                number = 0;
                sign = c;
            }
        }
        int ans = 0;
        while (!stack.isEmpty()) {
            ans += stack.pop();
        }
        return ans;
    }
//    public static void main(String[] args) {
//        FutureTask<Integer> futureTask = new FutureTask<>(()->{
//           return 1;
//        });
//    }
//
//    private static volatile int num = 0;
//    private static Object locker = new Object();
//    public static void main1(String[] args) throws InterruptedException {
//        Thread t = new Thread(() -> {
//            for (int i = 0; i < 50000; i++) {
//                synchronized (locker) {
//                    num++;
//                }
//            }
//        });
//        t.start();
//
//        for (int i = 0; i < 50000; i++) {
//            synchronized (locker) {
//                num++;
//            }
//        }
//
//        Thread.sleep(1000);
//        System.out.println("num = "+num);
//    }

}
